package com.xindaoapp.happypet.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.SplashPictureBean.SplashPicture;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.waterfall.ScaleImageView;
import com.xindaoapp.happypet.view.waterfall.util.ImageFetcher;

public class PictureAdapter extends BaseAdapter {

    private final ImageFetcher mImageFetcher;


    private List<SplashPicture> picList = new ArrayList<SplashPicture>();
    private final Context mContext;

    public PictureAdapter(Context context, List<SplashPicture> iniData) {
        this.picList = iniData;
        this.mContext = context;
        mImageFetcher = new ImageFetcher(context, 240);
        mImageFetcher.setLoadingImage(R.drawable.picture_loading);
        mImageFetcher.setExitTasksEarly(false);
    }

    public void clearItem() {
        if (picList != null) {
            picList.clear();
            // notifyDataSetChanged();
        }
    }

    public void addItem(List<SplashPicture> list) {
        this.picList.addAll(list);
        // notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (picList == null) {
            return 0;
        } else {
            return picList.size();
        }
    }

    @SuppressLint("InlinedApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_splash_picture2, null);
            holder.imageView = (ScaleImageView) convertView.findViewById(R.id.imageView);
            holder.userIcon = (CircleImageView) convertView.findViewById(R.id.userIcon);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            // holder.title.getBackground().setAlpha(100);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SplashPicture splashPicture = picList.get(position);
        int mHeight = 1;
        try {
            mHeight = Integer.parseInt(splashPicture.pics.get(0).height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int mWidth = 1;
        try {
            mWidth = Integer.parseInt(splashPicture.pics.get(0).width);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.imageView.setScaleType(ScaleType.FIT_XY);

        holder.imageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                (int) ((float) CommonParameter.sScreenWidth / 2 / mWidth * mHeight)));

        if (splashPicture.pics != null && splashPicture.pics.size() != 0
                && !TextUtils.isEmpty(splashPicture.pics.get(0).url)) {
            ImageLoader.getInstance().displayImage(splashPicture.pics.get(0).url, holder.imageView);
        } else {
            holder.imageView.setBackgroundResource(R.drawable.picture_loading);
        }
        ImageLoader.getInstance().displayImage(splashPicture.userface, holder.userIcon);
        holder.title.setText(splashPicture.author);
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, picList.get(position).tid);
                intent.putExtra(Constants.KEY_FROM_ACTIVITY, true);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }


    @Override
    public Object getItem(int position) {
        return picList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private class ViewHolder {
        ScaleImageView imageView;
        TextView title;
        CircleImageView userIcon;

    }

}
